﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NSDecisionTree.Gui
{
    public partial class CreateNewTable : Form
    {
        #region needed Vars

        List<string> listBoxItems = new List<string>();
        string currentSelected = String.Empty;

        public List<string> ListBoxItems
        {
            get { return listBoxItems; }
        }

        #endregion

        #region ctor

        public CreateNewTable()
        {
            InitializeComponent();
            listBox1.DataSource = listBoxItems;
            
        }

        #endregion

        #region Methods

        private void RefreshListBox()
        {
            listBox1.DataSource = listBoxItems;
            ((CurrencyManager)listBox1.BindingContext[listBox1.DataSource]).Refresh();
        }

        #endregion

        #region Menues and Events

        private void bt_Remove_Click(object sender, EventArgs e)
        {
            List<string> getDeletionItems = new List<string>();

            foreach (String item in listBox1.SelectedItems)
            {
                getDeletionItems.Add(item);
            }

            foreach (string item in getDeletionItems)
            {
                listBoxItems.Remove(item);
            }
            RefreshListBox();
        }

        private void bt_Up_Click(object sender, EventArgs e)
        {
            List<string> getSortItems = new List<string>();

            foreach (String item in listBox1.SelectedItems)
            {
                getSortItems.Add(item);
                currentSelected = item;
            }

            List<string> sortedList = new List<string>();
            for (int i = 0; i < listBoxItems.Count; i++)
            {
                if (i == listBoxItems.Count - 1)
                {
                    sortedList.Add(listBox1.Items[i].ToString());
                }
                else if (listBox1.SelectedItems.Contains(listBox1.Items[i + 1]))
                {
                    sortedList.Add(listBox1.Items[i + 1].ToString());
                    sortedList.Add(listBox1.Items[i].ToString());
                    i++;
                }
                else
                {
                    sortedList.Add(listBox1.Items[i].ToString());
                }
            }

            listBoxItems = null;
            listBoxItems = sortedList;
            RefreshListBox();
            listBox1.SelectedItem = currentSelected;
            currentSelected = String.Empty;
        }

        private void bt_Down_Click(object sender, EventArgs e)
        {
            List<string> getSortItems = new List<string>();

            foreach (String item in listBox1.SelectedItems)
            {
                getSortItems.Add(item);
                currentSelected = item;
            }

            List<string> sortedList = new List<string>();
            for (int i = 0; i < listBoxItems.Count; i++)
            {
                if (listBox1.SelectedItems.Contains(listBox1.Items[i]))
                {
                    if (i + 1 != listBoxItems.Count)
                        sortedList.Add(listBox1.Items[i + 1].ToString());

                    sortedList.Add(listBox1.Items[i].ToString());
                    i++;
                }
                else
                {
                    sortedList.Add(listBox1.Items[i].ToString());
                }
            }

            listBoxItems = sortedList;
            RefreshListBox();
            listBox1.SelectedItem = currentSelected;
            currentSelected = String.Empty;
        }

        private void bt_Add_Click(object sender, EventArgs e)
        {
            listBoxItems.Add(textBox1.Text);
            textBox1.Text = String.Empty;
            RefreshListBox();
            listBox1.SelectedItems.Clear();
            listBox1.SelectedIndices.Clear();
            textBox1.Focus();
        }

        #endregion

    }
}
